package com.jp.knowledge.my.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.my.adapter.OrderDetailAdapter;
import com.jp.knowledge.my.model.OrderModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/5.
 */

public class OrderDetailFragment extends BaseFragment {

    public static OrderDetailFragment newInstance(List<OrderModel> orderModels, int position){
        OrderDetailFragment orderDetailFragment = new OrderDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", (Serializable) orderModels);
        bundle.putInt("position", position);
        orderDetailFragment.setArguments(bundle);
        return orderDetailFragment;
    }

    private Context mContext;
    private View contentView;
    private CanRefreshLayout refreshLayout;
    private RecyclerView recyclerView;

    private OrderDetailAdapter adapter;
    private int position;
    private List<OrderModel> orderModels;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        orderModels = (List<OrderModel>) getArguments().getSerializable("data");
        position = getArguments().getInt("position");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(contentView == null){
            contentView = View.inflate(mContext, R.layout.refresh_recycleview, null);
            contentView.setBackgroundColor(getResources().getColor(R.color.gray_bg));
            refreshLayout = (CanRefreshLayout) contentView.findViewById(R.id.refresh);
            recyclerView = (RecyclerView) contentView.findViewById(R.id.can_content_view);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

            adapter = new OrderDetailAdapter(mContext, orderModels);
            recyclerView.setAdapter(adapter);

            refreshLayout.setRefreshEnabled(false);
            refreshLayout.setLoadMoreEnabled(false);
        }
        return contentView;
    }

    public void updateItemView(){
        adapter.notifyItemChanged(position);
    }
}
